﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS.DAL.Providers
{
    public class CustomerProvider : ProviderBase<Customer, CustomerSearchCriteria>
    {
        public override void Insert(Customer item)
        {
            Context.Customers.AddObject(item);
            Context.SaveChanges();
        }

        public override void Update(Customer updatedItem)
        {
            Customer obj = this.GetByID(updatedItem.ID);
            obj.Address = updatedItem.Address;
            obj.Credit = updatedItem.Credit;
            obj.DiscountPercentage = updatedItem.DiscountPercentage;
            obj.Email = updatedItem.Email;
            obj.Mobile = updatedItem.Mobile;
            obj.Name = updatedItem.Name;
            obj.Phone = updatedItem.Phone;
            Context.SaveChanges();
        }

        public override void Delete(int id)
        {
            Context.Customers.DeleteObject(this.GetByID(id));
            Context.SaveChanges();
        }

        public override Customer GetByID(int id)
        {
            return (from c in Context.Customers
                    where c.ID == id
                    select c).SingleOrDefault();
        }

        public override List<Customer> Search(CustomerSearchCriteria SearchCriteria)
        {
            switch (SearchCriteria.Status)
            {
                case 1:
                    return (from c in Context.Customers
                            where c.Name.Contains(SearchCriteria.Name) && c.Credit >0
                            select c).ToList();
                case 2:
                    return (from c in Context.Customers
                            where c.Name.Contains(SearchCriteria.Name) && c.Credit < 0
                            select c).ToList();
                default: 
                    return (from c in Context.Customers
                            where c.Name.Contains(SearchCriteria.Name)
                            select c).ToList();
                   
            }
        }

        public List<Customer> GetAll()
        {
            return (from c in Context.Customers
                    orderby c.Name
                    select c).ToList();
        }
    }
}